XML2Query Component
What Is the XML2Query Component?
XML2Query is a component for transforming XML into a query object. You can pass either a string of XML or an XML object to it and it will translate it into a query for you.
The advantage of XML2Query over WDDX is that the structure that XML2Query expects the XML to be in is much more human-readable than WDDX, which means it's ideal for storing application configuration information and other data that needs to be maintained later by hand.
Installing the XML2Query Component
The XML2Query component is contained in the com.macromedia.xml package, so the easiest way to install it is to create the directory structure com/macromedia/xml in your web root (if it isn't there already).
For instance, if your web root is wwwroot, then xml2query.cfc should be here:
wwwroot/com/macromedia/xml/xml2query.cfc
You can also place the component in a directory under a ColdFusion mapping or in a custom tag root.
Using the XML2Query Component
Using the XML2Query component is as simple as creating an instance using the cfobject tag or createObject function, setting either an XML string or an XML object on the component instance, and calling getQuery to have the resulting query returned to you. Below is an example:
<cfobject component="com.macromedia.xml.xml2query" name="xml2query" /> <cfinvoke component="#xml2query#" method="setXmlObject" xmlObject="#myXmlObj#" /> <cfinvoke component="#xml2query#" method="getQuery" returnVariable="xmlQuery" />
In order for an XML document to make sense as a query object, it most likely has a set of tags that repeats for every row in the query. The XML2Query component expects to find these sets of tags contained within the tag, which itself repeats for each row.
Below is an excerpt from an RSS XML document, which demonstrates a good XML format to be translated into a query object:
<item>
<title>Title #1 Here</title>
<link>http://www.server.com/path/to/post1.cfm</link>
<description>
This is a description of item #1.
</description>
<category>ColdFusion</category>
</item>
<item>
<title>Title #2 Here</title>
<link>http://www.server.com/path/to/post2.cfm</link>
<description>
This is a description of item #2.
</description>
<category>ColdFusion</category>
</item>
Here the item tags represent rows and the tags inside them represent columns. If use the XML2Query component to turn this XML into a query, it will have two rows with the Title, Link, Description and Category columns.
By default the XML2Query component looks for the first "dead-end" tag (tag with no children) and then assumes the tag before it contains the rows for the query. In the example above, when the component gets to the first title tag and finds that it cannot go any further, it assumes that the item tag (the tag before the title tag) represents the rows in your query.
In many cases, this is a safe assumption. However, consider the XML below:
<rss version="2.0">
<channel>
<title>Christian Cantrell's Perspective</title>
<link>http://www.macromedia.com/go/cantrell</link>
<description>Christian Cantrell's Weblog</description>
<item>
<title>Title #1 Here</title>
<link>http://www.server.com/path/to/post1.cfm</link>
<description>
This is a description of item #1.
</description>
<category>ColdFusion</category>
</item>
<item>
<title>Title #2 Here</title>
<link>http://www.server.com/path/to/post2.cfm</link>
<description>
This is a description of item #2.
</description>
<category>ColdFusion</category>
</item>
</channel>
</rss>
If the XML2Query component tries to parse the XML above into a query, it will only go as far as the title tag before assuming that the channel tag represents the rows in the query. This would give you a query with a single row with all the wrong columns. In instances where the default behavior of XML2Query is not desired, you can use the setStartingPoint function to indicate where in the XML document the XML2Query component should start looking for tags that represent rows.
The code below shows all the steps necessary to turn the XML example above into a query object:
<cffile action="read" file="path/to/xml/file.xml" variable="myDoc"/> <cfobject component="com.macromedia.xml.xml2query" name="xml2query" /> <cfinvoke component="#xml2query#" method="setXmlString" xmlObject="#myDoc#" /> <cfinvoke component="#xml2query#" method="setStartingPoint" startingPoint="/rss/channel/item" /> <cfinvoke component="#xml2query#" method="getQuery" returnVariable="xmlQuery" />
How the XML2Query Component Works
The XML2Query component works by either finding what it assumes is the most likely set of tags that represent rows in a query, or by going directly to the tags specified by the setStartingPoint function, and then iterating through the tags and creating rows in a new query object based on the tag names and the character data inside the tags.
If you need to access a repeating set of data in an XML document, using the XML2Query component is much faster and easier than accessing the data through ColdFusion's native XML API. Additionally, the XML2Query component works perfectly with its counterpart, the Query2XML component (included in DRK Volume 4), which gives you a way to save your query objects in a customizable, easy-to-read XML format.